<?php

namespace Home\Model;
use Think\Model;
class ParcelNoticeResultModel extends BaseModel{
    
    //短信微信记录表
    protected $tableName = 'tp_parcel_notice_result';
    
    /**
     * @desc 短信回调更改短信状态
     * @author quyang
     * @update 2016-12-1
     */
    public function updateSms($msg,$status,$error){
        $times = date('YmdHis',time());
        $where = array('f_sms_notice'=>$status,'f_sms_success_time'=>$times,'f_sms_error'=>$error);
        $this->where(array('f_parcel_id'=>$msg))->save($where);
    }
    
    /**
     * @desc 获取微信状态
     * @author quyang
     * @update 2016-12-2
     */
    public function weixin(){
        $map['f_weixin_notice'] = '2';
        $map['f_sms_notice'] = '0';
        $map['f_sms_num'] = '0';
        $curtime = time();
        $endtime = date('YmdHis', $curtime);
        $startime = date('YmdHis', $curtime - 12 * 60 * 60);
        $map['f_create_time'] = array('between', array($startime, $endtime));
        $data = $this->where($map)->getField('f_parcel_id',true);
        return $data;
    }
    
    /**
     * 查询微信和短信通知失败的进行先发微信，微信失败在发送短信
     * @author cheng
     */
    public function getParcelNoticeData() {
        $curtime = time();
        $endtime = date('YmdHis', $curtime);
        $startime = date('YmdHis', $curtime - 12 * 60 * 60);
        $map['f_create_time'] = array('between', array($startime, $endtime));
        $map['f_weixin_notice'] = 0;
        $map['f_weixin_num'] = 0;
        $result = D('Home/ParcelNoticeResult')->where($map)->select();
        return $result;
    }
    
    /**
     * 更新微信状态
     */
    public function updateParcelNotice($data) {
        $result = $this->where(array('f_parcel_id' => $data['f_parcel_id']))
                ->save(array('f_weixin_notice' => $data['f_weixin_notice'], 'f_weixin_success_time' => $data['f_weixin_success_time']));
        return $result;
    }

    /**
     * 更新微信状态
     * 提交更改状态
     */
    public function updateWeixinParcelNotice($data) {
        $result = $this->where(array('f_parcel_id' => $data['f_parcel_id']))
                ->save(array('f_weixin_notice' => $data['f_weixin_notice'],'f_weixin_send_time' => $data['f_weixin_send_time']));
        return $result;
    }

    /**
     * @desc 写入短信内容
     * @author quyang
     * @update 2016-12-6
     */
    public function updataContent($packageCode,$content=null,$status,$error){
        $times = date('YmdHis',time());
        $where = array('f_parcel_id'=>$packageCode,'f_sms_content'=>$content,'f_sms_notice'=>$status,'f_sms_send_time'=>$times,'f_sms_error'=>$error);
        $where['f_sms_num'] = ['exp','f_sms_num+1'];
        $this->save($where);
    }
    
    /**
     * @desc 扫描队列表获取发送快递公司数据
     * @author quyang
     * @update 2016-12-21
     */
    public function queryExpressData($totalRow){
        $data = $this->where($totalRow)->field('f_parcel_id,f_mail_no,f_station_id,f_station_name,f_tel')->select();
        return $data;
    }
    /**
     * @desc:
     * @Author: zhoutai
     * @update: 16/12/21
     **/
    public function queryExpressDatas($totalRow){
        $data = $this->where($totalRow)->field('f_parcel_id,f_mail_no,f_station_id,f_station_name,f_tel')->find();
        return $data;
    }

    /**
     * @desc 获取短信微信发送状况
     * @author quyang
     * @update 2016-12-24
     */
    public function sendNotice($method){
        $data = $this->where(array('f_parcel_id'=>$method))
            ->field('
                     f_parcel_id as fParcelId,
                     f_weixin_notice as fWeixinNotice,
                     f_sms_notice as fSmsNotice
                     ')->find();
        return $data;
    }

    /**
     * @desc 发送微信通知
     * @author quyang
     * @update 2016-12-24
     */
    public function getParcelData($parcelId) {
        $result = D('Home/ParcelNoticeResult')
            ->where(array('f_parcel_id'=>$parcelId))
            ->find();
        return $result ;
    }


    /**
     * @desc 获取短信微信发送状况批量
     * @author quyang
     * @update 2016-12-24
     */
    public function sendSmsNotice($method){
        $data = $this->where($method)
            ->field('
                     f_parcel_id as fParcelId,
                     f_weixin_notice as fWeixinNotice,
                     f_sms_notice as fSmsNotice
                     ')->select();
        return $data;
    }

}